package com.dahua.messageconsumer.sqlmq;

import com.dahua.common.dao.*;
import com.dahua.common.manager.SensorDataManager;
import com.dahua.messageconsumer.service.ActiveMqConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Slf4j
@Component
public class ConsumerRunner implements ApplicationRunner {
    /*
    # 并发 10 个SQL连接
    data.queueofsql=10
    public final String QUEUE_SQL = "data_sql";
    */

    @Autowired
    public ConsumerRunner(ActiveMqConfig activeMqConfig,
                          SensorDataManager sensorDataManager,
                          SensorDataDhMapper sensorDataDhMapper,
                          ElevatorMapper elevatorMapper,
                          LatestEquipmentStatusDao latestEquipmentStatusDao,
                          Control56Mapper control56Mapper,

                          EquipmentStatusMapper equipmentStatusMapper,
                          EquipmentStatusThisMonthMapper equipmentStatusThisMonthMapper,
                          EquipmentStatusTodayMapper equipmentStatusTodayMapper,
                          EquipmentStatusAlldayMapper equipmentStatusAlldayMapper,

                          DataLogMapper dataLogMapper,
                          LatestFaultMapper latestFaultMapper,
                          RuntimeParamMapper runtimeParamMapper,
                          FaultallMapper faultallMapper,
                          EquipmentFaultMapper equipmentFaultMapper,
                          EquipmentFaultDhMapper equipmentFaultDhMapper,
                          FaultMapper faultMapper,
                          SyncMessageMapper syncMessageMapper,
                          VersionInfoDao versionInfoDao,
                          ClientConfigparamsVersionUploadDao clientConfigparamsVersionUploadDao,
                          ClientConfigsUploadDao clientConfigsUploadDao,
                          ClientParamsUploadDao clientParamsUploadDao,
                          ClientAiParamsUploadDao clientAiParamsUploadDao,
                          ClientVibParamsUploadDao clientVibParamsUploadDao,
                          ClientPublicParamsUploadDao clientPublicParamsUploadDao,
                          ClientDmParamsUploadDao clientDmParamsUploadDao,
                          ClientAiVibPublicDmUploadTimeDao clientAiVibPublicDmUploadTimeDao,
                          EquipmentMapper equipmentMapper,
                          FaultInfoMapper faultInfoMapper
        ) {
        this.activeMqConfig = activeMqConfig;
        this.sensorDataManager = sensorDataManager;
        this.sensorDataDhMapper = sensorDataDhMapper;
        this.elevatorMapper = elevatorMapper;
        this.latestEquipmentStatusDao = latestEquipmentStatusDao;
        this.control56Mapper = control56Mapper;

        this.equipmentStatusMapper = equipmentStatusMapper;
        this.equipmentStatusThisMonthMapper = equipmentStatusThisMonthMapper;
        this.equipmentStatusTodayMapper = equipmentStatusTodayMapper;
        this.equipmentStatusAlldayMapper = equipmentStatusAlldayMapper;

        this.dataLogMapper = dataLogMapper;
        this.latestFaultMapper = latestFaultMapper;
        this.runtimeParamMapper = runtimeParamMapper;

        this.faultallMapper = faultallMapper;
        this.equipmentFaultMapper = equipmentFaultMapper;
        this.equipmentFaultDhMapper = equipmentFaultDhMapper;

        this.faultMapper = faultMapper;
        this.syncMessageMapper = syncMessageMapper;
        this.versionInfoDao = versionInfoDao;

        this.clientConfigparamsVersionUploadDao = clientConfigparamsVersionUploadDao;
        this.clientConfigsUploadDao = clientConfigsUploadDao;
        this.clientParamsUploadDao = clientParamsUploadDao;
        this.clientAiParamsUploadDao = clientAiParamsUploadDao;
        this.clientVibParamsUploadDao = clientVibParamsUploadDao;
        this.clientPublicParamsUploadDao = clientPublicParamsUploadDao;
        this.clientDmParamsUploadDao = clientDmParamsUploadDao;
        this.clientAiVibPublicDmUploadTimeDao = clientAiVibPublicDmUploadTimeDao;

        this.equipmentMapper = equipmentMapper;
        this.faultInfoMapper = faultInfoMapper;
    }

    private String topiceqdata = "data_sql";
    private String topiceqdata_first = "first_sql";
    private String topiceqdata_eqf = "eqf_sql";
    // 把 fault/faultall/latestfault/equipmentfault/equipmentfaultdh/control56 放入这里，以免sensordata耽误时间
    // 尤其是 fault，后续线网要反馈。如果这边还没有处理好，可能会有问题。

    @Value("${data.queueofsql}")
    private Integer queueofsql;
    @Value("${data.queueofsqlfirst}")
    private Integer queueofsqlfirst;
    @Value("${data.queueofsqleqf}")
    private Integer queueofsqleqf;
    @Value("${data.sqltrytimes}")
    private Integer sqltrytimes;
    // 故障邮件（仅内网）
    @Value("${data.websocket3}")
    private String websocket3;

    private ActiveMqConfig activeMqConfig;
    private SensorDataManager sensorDataManager;
    private SensorDataDhMapper sensorDataDhMapper;
    private ElevatorMapper elevatorMapper;
    private LatestEquipmentStatusDao latestEquipmentStatusDao;
    private Control56Mapper control56Mapper;

    private EquipmentStatusMapper equipmentStatusMapper;
    private EquipmentStatusThisMonthMapper equipmentStatusThisMonthMapper;
    private EquipmentStatusTodayMapper equipmentStatusTodayMapper;
    private EquipmentStatusAlldayMapper equipmentStatusAlldayMapper;

    private DataLogMapper dataLogMapper;
    private LatestFaultMapper latestFaultMapper;
    private RuntimeParamMapper runtimeParamMapper;

    private FaultallMapper faultallMapper;
    private EquipmentFaultMapper equipmentFaultMapper;
    private EquipmentFaultDhMapper equipmentFaultDhMapper;

    private FaultMapper faultMapper;
    private SyncMessageMapper syncMessageMapper;
    private VersionInfoDao versionInfoDao;

    private ClientConfigparamsVersionUploadDao clientConfigparamsVersionUploadDao;
    private ClientConfigsUploadDao clientConfigsUploadDao;
    private ClientParamsUploadDao clientParamsUploadDao;
    private ClientAiParamsUploadDao clientAiParamsUploadDao;
    private ClientVibParamsUploadDao clientVibParamsUploadDao;
    private ClientPublicParamsUploadDao clientPublicParamsUploadDao;
    private ClientDmParamsUploadDao clientDmParamsUploadDao;
    private ClientAiVibPublicDmUploadTimeDao clientAiVibPublicDmUploadTimeDao;

    private EquipmentMapper equipmentMapper;
    private FaultInfoMapper faultInfoMapper;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("data.queueofsql={}", queueofsql);
        log.info("data.queueofsqlfirst={}", queueofsqlfirst);
        log.info("data.queueofsqleqf={}", queueofsqleqf);

        {
            for (int i = 0; i < queueofsql; i++) {
                Consumer consumer = new Consumer(topiceqdata, i, sqltrytimes, activeMqConfig, websocket3,
                        sensorDataManager,
                        sensorDataDhMapper,
                        elevatorMapper,
                        latestEquipmentStatusDao,
                        control56Mapper,

                        equipmentStatusMapper,
                        equipmentStatusThisMonthMapper,
                        equipmentStatusTodayMapper,
                        equipmentStatusAlldayMapper,

                        dataLogMapper,
                        latestFaultMapper,
                        runtimeParamMapper,

                        faultallMapper,
                        equipmentFaultMapper,
                        equipmentFaultDhMapper,

                        faultMapper,
                        syncMessageMapper,
                        versionInfoDao,
                        clientConfigparamsVersionUploadDao,
                        clientConfigsUploadDao,
                        clientParamsUploadDao,
                        clientAiParamsUploadDao,
                        clientVibParamsUploadDao,
                        clientPublicParamsUploadDao,
                        clientDmParamsUploadDao,
                        clientAiVibPublicDmUploadTimeDao,
                        equipmentMapper,
                        faultInfoMapper
                        );
                Thread thread = new Thread(consumer);
                thread.setName(consumer.getQueue());
                thread.start();
            }
            for (int i = 0; i < queueofsqlfirst; i++) {
                Consumer consumer = new Consumer(topiceqdata_first, i, sqltrytimes, activeMqConfig, websocket3,
                        sensorDataManager,
                        sensorDataDhMapper,
                        elevatorMapper,
                        latestEquipmentStatusDao,
                        control56Mapper,

                        equipmentStatusMapper,
                        equipmentStatusThisMonthMapper,
                        equipmentStatusTodayMapper,
                        equipmentStatusAlldayMapper,

                        dataLogMapper,
                        latestFaultMapper,
                        runtimeParamMapper,

                        faultallMapper,
                        equipmentFaultMapper,
                        equipmentFaultDhMapper,

                        faultMapper,
                        syncMessageMapper,
                        versionInfoDao,
                        clientConfigparamsVersionUploadDao,
                        clientConfigsUploadDao,
                        clientParamsUploadDao,
                        clientAiParamsUploadDao,
                        clientVibParamsUploadDao,
                        clientPublicParamsUploadDao,
                        clientDmParamsUploadDao,
                        clientAiVibPublicDmUploadTimeDao,
                        equipmentMapper,
                        faultInfoMapper
                );
                Thread thread = new Thread(consumer);
                thread.setName(consumer.getQueue());
                thread.start();
            }
            for (int i = 0; i < queueofsqleqf; i++) {
                Consumer consumer = new Consumer(topiceqdata_eqf, i, sqltrytimes, activeMqConfig, websocket3,
                        sensorDataManager,
                        sensorDataDhMapper,
                        elevatorMapper,
                        latestEquipmentStatusDao,
                        control56Mapper,

                        equipmentStatusMapper,
                        equipmentStatusThisMonthMapper,
                        equipmentStatusTodayMapper,
                        equipmentStatusAlldayMapper,

                        dataLogMapper,
                        latestFaultMapper,
                        runtimeParamMapper,

                        faultallMapper,
                        equipmentFaultMapper,
                        equipmentFaultDhMapper,

                        faultMapper,
                        syncMessageMapper,
                        versionInfoDao,
                        clientConfigparamsVersionUploadDao,
                        clientConfigsUploadDao,
                        clientParamsUploadDao,
                        clientAiParamsUploadDao,
                        clientVibParamsUploadDao,
                        clientPublicParamsUploadDao,
                        clientDmParamsUploadDao,
                        clientAiVibPublicDmUploadTimeDao,
                        equipmentMapper,
                        faultInfoMapper
                );
                Thread thread = new Thread(consumer);
                thread.setName(consumer.getQueue());
                thread.start();
            }
        }
    }

    @PostConstruct
    public void ConsumerRunner() {

    }
}
